package com.chenyi.web.controller.system;

import com.chenyi.common.annotation.Log;
import com.chenyi.common.core.controller.BaseController;
import com.chenyi.common.core.page.TableDataInfo;
import com.chenyi.common.enums.BusinessType;
import com.chenyi.common.utils.poi.ExcelUtil;
import com.chenyi.system.domain.SysAdverPosition;
import com.chenyi.system.service.ISysAdverPositionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 广告位置Controller
 * 
 * @author qinggeng
 * @date 2020-02-12
 */
@RestController
@RequestMapping("/admin/position")
public class AdminAdverPositionController extends BaseController
{
    @Autowired
    private ISysAdverPositionService adverPositionService;

    /**
     * 查询广告位置列表
     */
    @PreAuthorize("@admin.hasPermi('system:position:list')")
    @GetMapping("/list")
    public ResponseEntity<TableDataInfo> list(SysAdverPosition adverPosition)
    {
        startPage();
        List<SysAdverPosition> list = adverPositionService.selectSysAdverPositionList(adverPosition);
        return new ResponseEntity<>(getDataTable(list), HttpStatus.OK);
    }

    /**
     * 导出广告位置列表
     */
    @PreAuthorize("@admin.hasPermi('system:position:export')")
    @Log(title = "广告位置", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public ResponseEntity<Object> export(SysAdverPosition adverPosition)
    {
        List<SysAdverPosition> list = adverPositionService.selectSysAdverPositionList(adverPosition);
        ExcelUtil<SysAdverPosition> util = new ExcelUtil<SysAdverPosition>(SysAdverPosition.class);
        return util.exportExcel(list, "position");
    }

    /**
     * 获取广告位置详细信息
     */
    @PreAuthorize("@admin.hasPermi('system:position:query')")
    @GetMapping(value = "/{positionId}")
    public ResponseEntity<Object> getInfo(@PathVariable("positionId") Long positionId){
        return ResponseEntity.ok(adverPositionService.selectSysAdverPositionById(positionId));
    }

    /**
     * 新增广告位置
     */
    @PreAuthorize("@admin.hasPermi('system:position:add')")
    @Log(title = "广告位置", businessType = BusinessType.INSERT)
    @PostMapping
    public ResponseEntity<Object> add(@RequestBody SysAdverPosition adverPosition)
    {
        adverPositionService.insertSysAdverPosition(adverPosition);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    /**
     * 修改广告位置
     */
    @PreAuthorize("@admin.hasPermi('system:position:edit')")
    @Log(title = "广告位置", businessType = BusinessType.UPDATE)
    @PutMapping
    public ResponseEntity<Object> edit(@RequestBody SysAdverPosition adverPosition)
    {
        adverPositionService.updateSysAdverPosition(adverPosition);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 删除广告位置
     */
    @PreAuthorize("@admin.hasPermi('system:position:remove')")
    @Log(title = "广告位置", businessType = BusinessType.DELETE)
	@DeleteMapping("/{positionIds}")
    public ResponseEntity<Object> remove(@PathVariable Long[] positionIds)
    {
        adverPositionService.deleteSysAdverPositionByIds(positionIds);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}
